<?php
/**
 * Created by PhpStorm.
 * Script Name: Bbase.php
 * Create: 2021/12/21 12:37
 * Description: 客户功能基础类
 * Author: fudaoji<fdj@kuryun.cn>
 */

namespace app\admin\controller;
use app\admin\model\Admin as AdminM;
use app\common\service\AdminLog as AdminLogService;
use app\constants\Media;

class Bbase extends Base
{
    protected $needAid = true;
    protected $needStaffId = false;

    public function initialize()
    {
        parent::initialize(); // TODO: Change the autogenerated stub
    }

    /**
     * 设置一条或者多条数据的状态
     * @Author  fudaoji<fdj@kuryun.cn>
     */
    public function setStatus() {
        $ids = input('ids/a');
        $status = input('status');

        if (empty($ids)) {
            $this->error('请选择要操作的数据');
        }

        $ids = (array) $ids;
        if($status == 'delete'){
            $map = ['id' => ['in', $ids]];
            $this->needAid && $map['admin_id'] = $this->adminInfo['id'];
            $this->needStaffId && $map['staff_id'] = $this->adminInfo['id'];
            if($this->model->delByMap($map)){
                AdminLogService::addLog(['year' => (int)date('Y'), 'type' => AdminLogService::DEL, 'desc' => $this->adminInfo['username'] . '删除数据'.$this->model->getName().':'.implode(',', $ids)]);
                $this->success('删除成功');
            }else{
                $this->error('删除失败');
            }
        }else{
            $arr = [];
            $msg = [
                'success' => '操作成功！',
                'error'   => '操作失败！',
            ];
            switch ($status) {
                case 'forbid' :  // 禁用条目
                    $data['status'] = 0;
                    break;
                case 'resume' :  // 启用条目
                    $data['status'] = 1;
                    break;
                default:
                    $this->error('参数错误');
                    break;
            }
            /*if(!is_null($val = input('val', null))){
                $data['status'] = abs($val - 1);
            }*/
            foreach($ids as $id){
                $data[$this->pk] = $id;
                $this->needAid && $data['admin_id'] = $this->adminInfo['id'];
                $arr[] = $data;
            }
            if($this->model->saveAll($arr)){
                $this->success($msg['success']);
            }else{
                $this->error($msg['error']);
            }
        }
    }

    /**
     * 保存数据
     * @param string $jump_to
     * @param array $data
     * @return mixed
     * @throws \think\Exception
     */
    public function savePost($jump_to = '/undefined', $data=[]){
        empty($jump_to) && $jump_to = '/undefined';
        $post_data = $data ? $data : input('post.');
        if(empty($post_data[$this->pk])){
            $this->needAid && $post_data['admin_id'] = $this->adminInfo['id'];
            $res = $this->model->addOne($post_data);
        }else {
            $res = $this->model->updateOne($post_data);
        }

        if($res){
            $this->success('数据保存成功', $jump_to);
        }else{
            $this->error('数据保存出错');
        }
    }

    public function mediaTabs(){
        return [
            Media::TEXT => ['title' => '文本', 'href' => url('mediatext/index')],
            Media::IMAGE => ['title' => '图片', 'href' => url('mediaimage/index')],
            Media::FILE => ['title' => '文件', 'href' => url('mediafile/index')],
            Media::VIDEO => ['title' => '视频', 'href' => url('mediavideo/index')],
            Media::LINK => ['title' => '分享链接', 'href' => url('medialink/index')],
            Media::XML => ['title' => 'XML卡片', 'href' => url('mediaxml/index')]
        ];
    }

    /**
     * 刷新素材
     * @param int $id
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * Author: fudaoji<fdj@kuryun.cn>
     */
    protected function refreshMedia($id = 0){
        $this->model->getOneByMap(['admin_id' => ['in', [$this->adminInfo['id'], AdminM::getCompanyId($this->adminInfo)]], 'id' => $id], true, true);
    }
}